<?php
	class WxAppliModel extends CI_Model {
		// 表名
		static $wxAppliTable = "wx_application";
		static $wxUserAppliTable = "wx_user_application";
		static $wxBannerTable = "wx_banner";

	    public function __construct() {
	        parent::__construct();
	    }

	    // 获取应用信息
	    public function get_wx_application($id){
	    	$this->db->where("id", $id);
	    	return $this->db->get(self::$wxAppliTable)->row_array();
		}
		
		// 根据value获取应用信息
		public function get_wx_application_by_value($value, $bindUserType){
			$this->db->where('is_delete', 0);
			$this->db->where("value", $value);
			$this->db->where("bind_user_type", $bindUserType);
			return $this->db->get(self::$wxAppliTable)->row_array();
		}

		// 根据custom_menu_id获取应用信息
		public function get_wx_application_by_custom_menu($customMenuId, $bindUserType){
			$this->db->where('is_delete', 0);
			$this->db->where("custom_menu_id", $customMenuId);
			$this->db->where("bind_user_type", $bindUserType);
			return $this->db->get(self::$wxAppliTable)->row_array();
		}

        // 获取应用列表
        public function get_wx_application_list($params) {
            // 用户类型
			if(isset($params['bindUserType'])) {
				$this->db->where('bind_user_type', $params['bindUserType']);
			}
			if(isset($params['isDelete'])) {
				$this->db->where('is_delete', $params['isDelete']);
			}
			if(isset($params['customMenuId'])) {
				$this->db->where("custom_menu_id", $params['customMenuId']);
			}
			// 关联后台admin_menu表
			if(isset($params['adminMenuIdArr'])) {
				$this->db->where_in('admin_menu_id', $params['adminMenuIdArr']);
			}
			// 排除某个应用名称
			if(isset($params['excludeTitleArr']) && $params['excludeTitleArr']) {
				$this->db->where_not_in('title', $params['excludeTitleArr']);
			}
			// 包含某个应用名称
			if(isset($params['includeTitleArr'])) {
				$this->db->where_in('title', $params['includeTitleArr']);
			}
			// 排序
			if(isset($params['orderBy'])) {
				$this->db->order_by($params['orderBy']);
			}
            return $this->db->get(self::$wxAppliTable)->result_array();
		}
		
	    // 获取用户应用列表
	    public function get_wx_user_appli_list($params){
			if(isset($params['appliId'])) {
				$this->db->where("FIND_IN_SET('{$params['appliId']}', `appli_ids`)");
			}
            // 用户类型
			if(isset($params['bindUserType'])) {
				$this->db->where('bind_user_type', $params['bindUserType']);
            }
            // 用户id
			if(isset($params['userId'])) {
				$this->db->where('user_id', $params['userId']);
			}
            // 是否删除
			if(isset($params['isDelete'])) {
				$this->db->where('is_delete', $params['isDelete']);
			}
			
	    	return $this->db->get(self::$wxUserAppliTable)->result_array();
		}

	    // 获取用户应用信息
	    public function get_wx_user_appli($params){
			if(isset($params['id'])) {
				$this->db->where("id", $params['id']);
			}
            // 用户类型
			if(isset($params['bindUserType'])) {
				$this->db->where('bind_user_type', $params['bindUserType']);
            }
            // 用户id
			if(isset($params['userId'])) {
				$this->db->where('user_id', $params['userId']);
			}
            // 是否删除
			if(isset($params['isDelete'])) {
				$this->db->where('is_delete', $params['isDelete']);
			}
			
	    	return $this->db->get(self::$wxUserAppliTable)->row_array();
		}
		
	    // 获取移动端轮播图列表
	    public function get_banner_list($params = ""){
	    	// 查询
	    	$where = "1 AND 1";
	    	if(isset($params['schoolId'])) $where .= " AND `school_id`='{$params['schoolId']}'";
	    	if(isset($params['campusId'])) $where .= " AND `campus_id`='{$params['campusId']}'";
	    	// 删除状态
	    	if(isset($params['isDelete'])) $where .= " AND `is_delete`='{$params['isDelete']}'";
	    	$this->db->where($where);
	    	// 分页
	    	if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
	    	// 是否只获取条数
	    	if(isset($params['getRowNumber'])) return $this->db->count_all_results(self::$wxBannerTable);
	    	return $this->db->order_by("`banner_sort` DESC")->get(self::$wxBannerTable)->result_array();
	    }

    }